/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Comp_12_ColorChooser.java]
 * Creation Date        [23-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_05_swing.components;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Swing 拾色器组件
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Comp_12_ColorChooser {

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				ColorChooserFrame frame = new ColorChooserFrame();
				frame.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}
		});
	}
}

/**
 * 拾色器窗体
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 */
class ColorChooserFrame extends JFrame {

	private static final long serialVersionUID = 2469814564491394109L;

	/**
	 * 构造方法
	 */
	public ColorChooserFrame() {
		setTitle("Color Chooser");
		this.setSize(300, 200);
		setLocationByPlatform(true);
		setResizable(false);

		ColorChooserPanel panel = new ColorChooserPanel();
		getContentPane().add(panel);
	}
}

/**
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 */
class ColorChooserPanel extends JPanel {

	private static final long serialVersionUID = 9176011822670438230L;

	/**
	 * 构造方法
	 */
	public ColorChooserPanel() {
		JButton modalButton = new JButton("Modal");
		modalButton.addActionListener(new ModalListener());
		this.add(modalButton);

		JButton modelessButton = new JButton("Modeless");
		modelessButton.addActionListener(new ModelessListener());
		this.add(modelessButton);

		JButton immediateButton = new JButton("Immediate");
		immediateButton.addActionListener(new ImmediateListener());
		this.add(immediateButton);
	}

	/**
	 * 模式化颜色选择器
	 * 
	 * @author 不落的太阳(Sean Yang)
	 * @version 1.0
	 * @since JDK 6
	 */
	private class ModalListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent event) {
			Color defaultColor = getBackground();
			Color selectedColor = JColorChooser.showDialog(
					ColorChooserPanel.this, "背景色", defaultColor);
			if (selectedColor != null) {
				setBackground(selectedColor);
			}
		}
	}

	/**
	 * 无模式化颜色选择器
	 * 
	 * @author 不落的太阳(Sean Yang)
	 * @version 1.0
	 * @since JDK 6
	 */
	private class ModelessListener implements ActionListener {

		private JDialog dialog = null;
		private JColorChooser chooser = null;

		public ModelessListener() {
			chooser = new JColorChooser();
			dialog = JColorChooser.createDialog(ColorChooserPanel.this, "背景色",
					false, chooser, new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent event) {
							setBackground(chooser.getColor());
						}
					}, null);
		}

		@Override
		public void actionPerformed(ActionEvent event) {
			chooser.setColor(getBackground());
			dialog.setVisible(true);
		}
	}

	/**
	 * 立即更新颜色选择器
	 * 
	 * @author 不落的太阳(Sean Yang)
	 * @version 1.0
	 * @since JDK 6
	 */
	private class ImmediateListener implements ActionListener {
		private JDialog dialog = null;
		private JColorChooser chooser = null;

		public ImmediateListener() {
			chooser = new JColorChooser();
			chooser.getSelectionModel().addChangeListener(new ChangeListener() {
				@Override
				public void stateChanged(ChangeEvent event) {
					setBackground(chooser.getColor());
				}
			});

			dialog = new JDialog((Frame) null, false);
			dialog.add(chooser);
			dialog.pack();
		}

		@Override
		public void actionPerformed(ActionEvent event) {
			chooser.setColor(getBackground());
			dialog.setVisible(true);
		}
	}
}
